<template>
    <view class="p20">
        <uni-forms ref="formRef" :model="formData" :rules="rules" :label-width="80">
            <!-- 任务名称 -->
            <uni-forms-item label="任务名称" required name="Name">
                <uni-easyinput v-model="formData.Name" placeholder="请输入任务名称" trim="both" />
            </uni-forms-item>
            <!-- 任务内容 -->
            <uni-forms-item label="任务内容" name="Content">
                <uni-easyinput type="textarea" v-model="formData.Content" placeholder="请输入任务内容" autoHeight />
            </uni-forms-item>
            <!-- 任务类型编码 -->
            <uni-forms-item label="任务类型" required name="TypeCode">
                <uni-data-select v-model="formData.TypeCode" :localdata="dictData.task_type" placeholder="请选择任务类型" />
            </uni-forms-item>
            <!-- 截止时间 -->
            <uni-forms-item label="截止时间" required name="EndTime">
                <uni-datetime-picker type="date" v-model="formData.EndTime" placeholder="请选择截止日期" />
            </uni-forms-item>
            <!-- 任务指定用户 -->
            <uni-forms-item label="指定用户" required name="ToDoUserId">
                <uni-data-select v-model="formData.ToDoUserId" :localdata="childrenList" placeholder="请选择指定用户" />
            </uni-forms-item>
            <uv-button type="primary" @click="submitForm" :loading="loading" loadingSize="30rpx"
                v-if="formOperType != SYSFORMOPERTYPE.View">提交报表</uv-button>
        </uni-forms>
    </view>
</template>

<script lang="ts" setup>
import { useUserChildrenList } from '@/hooks/useOptions'
import { CreateByTown_API, SysTaskLook_API, updateByTown_API } from '@/server/home'
import { useDictStore } from '@/stores'
import { SYSFORMOPERTYPE } from '@/utils/enum'
const formData = ref({
    Name: '',
    Content: '',
    TypeCode: '',
    EndTime: '',
    ToDoUserId: null
})
const rules = {
    Name: {
        rules: [{
            required: true,
            errorMessage: '任务名称不能为空'
        }]
    },
    TypeCode: {
        rules: [{
            required: true,
            errorMessage: '请选择任务类型'
        }]
    },
    EndTime: {
        rules: [{
            required: true,
            errorMessage: '请选择截止时间'
        }]
    },
    ToDoUserId: {
        rules: [{
            required: true,
            errorMessage: '用户ID不能为空'
        },
        {
            format: 'number',
            errorMessage: '必须为数字类型'
        }
        ]
    }
}

const dictStore = useDictStore()
const { dictData } = storeToRefs(dictStore)

const { childrenList, fetchUserChildrenList } = useUserChildrenList()
let taskId = ''
const formOperType = ref()

onLoad((options) => {
    taskId = options.id
    formOperType.value = options.optype || SYSFORMOPERTYPE.Add
    if (taskId) initTask()
    //请求字典数据
    dictStore.getDictData(['disaster_type', 'task_type'])
    fetchUserChildrenList(1)
})
const loading = ref(false)
const formRef = ref()
const submitForm = async () => {
    try {
        await formRef.value.validate();
        loading.value = true
        let res = null
        if (formOperType.value == SYSFORMOPERTYPE.Add) {
            res = await CreateByTown_API(formData.value)
        } else {
            res = await updateByTown_API({ ...formData.value, id: taskId }, taskId)
        }
        if (res.IsSuccess) {
            uni.$uv.toast('提交成功')
            setTimeout(() => {
                uni.$emit('refresh-data')
                uni.navigateBack()
            }, 1500)
        } else {
            uni.$uv.toast(res.Message || '提交失败')
        }
    } catch (error) {
        uni.$uv.toast(error.message || '提交失败')
    } finally {
        loading.value = false
    }
}

const initTask = async () => {
    const res = await SysTaskLook_API(taskId)
    if (res.IsSuccess) {
        formData.value = res.Data
    }
}
</script>

<style lang="scss" scoped>
//</style>
